﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Dao
{
    public class Departments_Dao
    {
        /// <summary>
        /// 新增一条记录
        /// </summary>
        /// <param name="entity">记录实体</param>
        /// <returns>操作影响的行数</returns>
        public int Add(Department entity)
        {
            using (var context = new Dao.ErpEntities())
            {
                context.DepartmentSet.AddObject(entity);
                return context.SaveChanges();
            }
        }

        /// <summary>
        /// 更新一条记录
        /// </summary>
        /// <param name="entity">记录实体</param>
        /// <returns>操作影响的行数</returns>
        public int Update(Department entity)
        {
            using (var context = new Dao.ErpEntities())
            {
                Department entity2 = context.DepartmentSet.FirstOrDefault(c => c.Id == entity.Id);
                if (entity2 != null)
                {
                    entity2 = entity;
                    context.DepartmentSet.ApplyCurrentValues(entity);
                    return context.SaveChanges();
                }
                return 0;
            }
        }

        /// <summary>
        /// 删除一条记录
        /// </summary>
        /// <param name="id">记录Id</param>
        /// <returns>操作影响的行数</returns>
        public int Delete(string id)
        {
            using (var context = new Dao.ErpEntities())
            {
                Guid guid = Guid.Parse(id);
                Department entity = context.DepartmentSet.FirstOrDefault(c => c.Id == guid);
                if (entity != null)
                {
                    context.DeleteObject(entity);
                    return context.SaveChanges();
                }
                return 0;
            }
        }

        /// <summary>
        /// 取根部门
        /// </summary>
        /// <param name="id">记录Id</param>
        /// <returns>查询结果实体</returns>
        public Department GetRoot()
        {
            using (var context = new Dao.ErpEntities())
            {
                Department entity = context.DepartmentSet.FirstOrDefault(c => c.ParentId == null);
                return entity;
            }
        }

        public List<Department> GetChildren(string id)
        {
            List<Department> list = new List<Department>();
            using (var context = new Dao.ErpEntities())
            {
                Guid guid = Guid.Parse(id);
                var q = from c in context.DepartmentSet
                        where c.ParentId == guid
                        select c;
                foreach (var c in q)
                {
                    list.Add(c);
                }
                return list;
            }
        }

        public List<User> GetUsers(string id)
        {
            List<User> list = new List<User>();
            using (var context = new Dao.ErpEntities())
            {
                Guid guid = Guid.Parse(id);
                var q = from u in context.UserSet
                        join ud in context.UserInDepartmentSet
                        on u.Id equals ud.UserId
                        where ud.DepartmentId == guid
                        select u;
                foreach (var c in q)
                {
                    list.Add(c);
                }
                return list;
            }
        }

        /// <summary>
        /// 查询一条记录
        /// </summary>
        /// <param name="id">记录Id</param>
        /// <returns>查询结果实体</returns>
        public Department Query(string id)
        {
            using (var context = new Dao.ErpEntities())
            {
                Guid guid = Guid.Parse(id);
                Department entity = context.DepartmentSet.FirstOrDefault(c => c.Id == guid);
                return entity;
            }
        }

        /// <summary>
        /// 查询所有记录
        /// </summary>
        /// <returns>查询结果集</returns>
        public List<Department> QueryAll()
        {
            List<Department> list = new List<Department>();
            using (var context = new Dao.ErpEntities())
            {
                var q = from c in context.DepartmentSet
                        select c;
                foreach (var c in q)
                {
                    list.Add(c);
                }
                return list;
            }
        }
    }
}
